﻿
$(document).ready(function () {

    $('#tbCustomer').dataTable({ "oLanguage":
                 { "sUrl": "/jquery/jquery.dataTable.cn.txt" },
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        'bPaginate': true,
        "bProcessing": true,
        "iDisplayLength": 20,
        "bSort": true,
        "aaSorting": [[0, "desc"]]
    });

    SetDisply();
});



function showbussinessBycity(obj, isfirst) {
        var strhtml = $("#tabpoint").html();
        var newhtml = "";
        var citycode = $(obj).parent().attr("code");
        var said = $(".hidareaid").val();
        var v  = "";
        if ($(obj).attr("checked") == "checked") {
            $.ajax({
                type: "post",
                url: "/ashx/Project.ashx",
                data: { city: citycode, areaid: said, isnew: isfirst },
                success: function (msg) {
                    newhtml = strhtml + msg;
                    $("#tabpoint").append(msg);

                    v = $(".cjoview").val();
                 
                    if (isfirst == 1) {
                        v = GetArrUser(v, citycode, 4, "", "");
                    }
                 

                    $(".cjoview").val(v);

                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    alert("Error;" + XmlHttpRequest.responseText);
                }
            });
        }
        else {

            if (isfirst == 1) {
                $("." + citycode).remove();


                //删除hid里对应的值
                v = $(".cjoview").val();
                v = GetArrUser(v, citycode, 3, "", ""); //删除可查看业务员
                $(".cjoview").val(v);

                //删除授权业务员
                v = $(".cjoauth").val();
                delCityMan(v, citycode, 'cjoauth');
                $(".cjoauth").val(v);


                //删除显示业务员
                v = $(".cjoshow").val();
                delCityMan(v, citycode, 'cjoshow');
                $(".cjoshow").val(v);

                //删除开CJO人员
                v = $(".cjocreate").val();
                delCityMan(v, citycode, 'cjocreate');
                $(".cjocreate").val(v);
            }

        }


    }

    
    function GetArrByCity(val) {
        var arrcity = val.split('#');
        return arrcity;
    }

    //type:0去掉业务员，1加业务员,3,删除城市,4是新增城市
    function GetArrUser(val, city, type, username, truename) {
   
        var arrcity = GetArrByCity(val);
        var strusername="";
        var strtruename="";
        var newval = "";
     
        if (type == 4) {
            newval = val + city + "{" + ";" + "}#";
        }
        else {
            if (val == "" && type == 1) {

                strusername = strusername + username + "@";
                strtruename = strtruename + truename + "@";
                newval =  city + "{" + strusername + ";" + strtruename + "}#";                
            }
            $.each(arrcity, function (i, v) {

                if (v.indexOf(city) != -1) {
                    strusername = v.substr(v.indexOf("{") + 1, v.indexOf(";") - v.indexOf("{") - 1);
                    strtruename = v.substr(v.indexOf(";") + 1, v.indexOf("}") - v.indexOf(";") - 1);
                    if (type == 0) {
                       strusername= strusername.replace(username + "@", "");
                       strtruename= strtruename.replace(truename + "@", "");
                        newval = newval + city + "{" + strusername + ";" + strtruename + "}#";
                    }
                    else if (type == 1) {
                        strusername = strusername + username + "@";
                        strtruename = strtruename + truename + "@";
                        newval = newval + city + "{" + strusername + ";" + strtruename + "}#";
                    }
                    else if (type == 3) {
                        newval = newval + "";

                    }
                    else if (type == 4) {
                        newval = val + city + "{" + ";" + "}#";
                    }

                } else {

                    if (v != null && v != "")
                        newval = newval + v + "#";

                }

            });
        }
        return newval;
    }


    function Addcjoview(obj) {
        //
        //GetArrUser(val, city, type, username, truename)
        var oldval = $(".cjoview").val();
        var username = $(obj).attr("alt");
        var truename = $(obj).attr("title");
        var citycode = $(obj).attr("cc");
        var newval = "";

        if ($(obj).attr("checked") == "checked") {
            newval = GetArrUser(oldval, citycode, 1, username, truename);
        }
        else {
            newval = GetArrUser(oldval, citycode, 0, username, truename);
       }
       $(".cjoview").val(newval);
     
    }

    //isdel 0删除城市(从checkboxlist删除，取消hid值) 1是增加
    function GetArrCity(hidcss, obj, city) {

        var v = $("." + hidcss).val();
        var o = $(".cjoauth" + city);
        var arrcity = val.split(';');

    }


    //当取消城市的选择时
    function delCityMan(strval, city, hidcss) {
        var newhtml = "";
        $.each(strval.split(';'), function (i, v) {
            if (v.indexOf(city) == -1) {
                newhtml = newhtml + v + ";";
            }

        });
        $("." + hidcss).val(newhtml);
    }

    //00002{SH0004,遥遥};00003{BJ0005,的人};
    //选择城市以后替换
    function onchangeByname(hidcss,city,selectid) {
        var newhtml = "";
        var strval = $("." + hidcss).val();
       
        if (strval == null || strval == "") {
            newhtml = newhtml + city + "{";
            newhtml = newhtml + $("#" + selectid).val() + ",";
            newhtml = newhtml + $("#" + selectid).find("option:selected").text();
            newhtml = newhtml + "};";
        }
        else {

            var flag = 0;
            $.each(strval.split(';'), function (i, v) {
                
                if (v != null && v != "") {
                    var isexits = v.indexOf(city);

                    if (isexits == -1) {
                        newhtml = newhtml + v + ";";
                    }
                    else {
                    flag = 1;
                    //已经存在，替换
                        newhtml = newhtml + city + "{";
                        newhtml = newhtml + $("#" + selectid).val() + ",";
                        newhtml = newhtml + $("#" + selectid).find("option:selected").text();
                        newhtml = newhtml + "};";
                    }
                }

            });
            //
            if(flag==0){
                        newhtml = newhtml + city + "{";
                        newhtml = newhtml + $("#" + selectid).val() + ",";
                        newhtml = newhtml + $("#" + selectid).find("option:selected").text();
                        newhtml = newhtml + "};";
                        
            }
                }
                //alert(newhtml);
        $("." + hidcss).val(newhtml);
    }


    function SetDisply() {

        $('.csspoint input[type=checkbox]').each(function () {
            showbussinessBycity(this,0);
        });
    }
   
